package com.vieewer.btdemo.view.gdmap.preview

import android.content.Context
import android.hardware.Sensor
import android.hardware.SensorEvent
import android.hardware.SensorEventListener
import android.hardware.SensorManager
import android.os.Bundle
import android.support.v4.content.ContextCompat
import android.support.v7.widget.LinearLayoutManager
import android.util.Log
import android.view.View
import android.view.WindowManager
import com.alibaba.fastjson.JSON
import com.vieewer.btdemo.R
import com.vieewer.btdemo.base.BaseActivity
import com.vieewer.btdemo.bean.ChuanGDPlanBean
import com.vieewer.btdemo.bean.SwitchMsgBean
import com.vieewer.btdemo.utils.CommunicateToPhoneHelper
import com.vieewer.btdemo.utils.LogUtil
import com.vieewer.btdemo.view.gdmap.GdMapPlanBean
import com.vieewer.btdemo.view.gdmap.GdSegmentListAdapter
import com.vieewer.btdemo.view.info.SugonGlassesInfoManager
import kotlinx.android.synthetic.main.activity_gd_map_preview.*

class GdMapPreviewActivity : BaseActivity() {

    var mData = ArrayList<GdMapPlanBean>()
    private var sensorManager: SensorManager? = null
    var direction = ""

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_gd_map_preview)
        window.addFlags(
              WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD or
                    WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON or
                    WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON)

        initRecycleView()
        registerSensor()
        initListener()

    }

    private fun initListener() {
        c_txt_start_guide.setOnClickListener{

            val data = SwitchMsgBean()
            data.type = "startMapGuide"
            val dataStr = JSON.toJSONString(data)
            LogUtil.e("yjj", "开始导航$dataStr")
            CommunicateToPhoneHelper.instance.write(dataStr)

            this.finish()
        }
    }


    private fun initRecycleView() {
        if (intent.getSerializableExtra("data") != null) {

            var dataBean = intent.getSerializableExtra("data") as ChuanGDPlanBean
            if (dataBean != null) {
                mData.addAll(dataBean.gdMapPlanBeans)
            }

        }

        guide_recyclerview.layoutManager = LinearLayoutManager(this)
        guide_recyclerview.adapter = GdSegmentListAdapter(this, mData)
        c_txt_start_guide.visibility = if (mData.size > 0) View.VISIBLE else View.GONE

    }


    private fun registerSensor() {

        sensorManager = getSystemService(Context.SENSOR_SERVICE) as SensorManager
        sensorManager!!.registerListener(mEventListener,
            sensorManager!!.getDefaultSensor(Sensor.TYPE_ORIENTATION), SensorManager.SENSOR_DELAY_GAME)

    }



    override fun onDestroy() {
        super.onDestroy()
        if (sensorManager != null) {
            sensorManager!!.unregisterListener(mEventListener)
        }
    }


    val mEventListener = object : SensorEventListener {

        override fun onSensorChanged(sensorEvent: SensorEvent) {

            var angle = sensorEvent.values[0]

            direction = SugonGlassesInfoManager.instance.getSensorDirection(angle)

            Log.i("yjj", "direction =$direction")


            if (direction == mData[0].orien) {
                c_txt_start_guide.isEnabled = true
                c_txt_start_guide.setTextColor(ContextCompat.getColor(this@GdMapPreviewActivity, R.color.white))
                c_txt_start_guide.setBackgroundResource(R.drawable.shape_start_map_guide_bg_h)

            } else {
                c_txt_start_guide.isEnabled = false
                c_txt_start_guide.setTextColor(ContextCompat.getColor(this@GdMapPreviewActivity, R.color.deepGray))
                c_txt_start_guide.setBackgroundResource(R.drawable.shape_start_map_guide_bg_n)
            }


        }

        override fun onAccuracyChanged(sensor: Sensor, i: Int) {

        }
    }


}